/*************************************************************************
    > File Name: 2.client.c
    > Author: Ykk
    > Mail: 3393579535@qq.com 
    > Created Time: 2023年05月15日 星期一 22时11分49秒
 ************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<unistd.h>


int main(int argc,char *argv[])
{
	//创建套接字
	int sock_fd;
	int ret = -1;
	sock_fd = socket(AF_INET,SOCK_STREAM,0);
	if(-1 == sock_fd)
	{
		perror("sock_fd");
		return 1;
	}
	else
	{
		printf("创建套接字成功\n");
	}
	//连接服务器
	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(8000);
	inet_pton(AF_INET,"192.168.152.129",&addr.sin_addr.s_addr);
	ret = connect(sock_fd,(struct sockaddr *)&addr,sizeof(addr));
	if(-1 == ret)
	{
		perror("connect");
		return 1;
	}
	else
	{
		printf("连接成功...\n");
	}
	//读写数据
	char buf[1024] = "";
	while(1)
	{
		bzero(buf,sizeof(buf));
		int n = read(STDIN_FILENO,buf,sizeof(buf));
		write(sock_fd,buf,n);//发送数据给服务器
		n = read(sock_fd,buf,sizeof(buf));
		write(STDOUT_FILENO,buf,n);
	}
	//关闭
	close(sock_fd);
	return 0;
}
